﻿using UnityEngine;
using ZCUtil.Singleton;
using ZCManager;
using ZCMain;
using ZCTouchDrawBoard;
using ZCUIManager;

public class ProgramManager : Singleton<ProgramManager>
{
    #region PrivateField
    private static GameObject m_uiCanvasGo;
    private static RectTransform m_uiCanvasRectTransform;
    private static Camera m_mainCamera;
    private static MainCanvasRender m_mainCanvasRender;
    private static GameObject m_mainCanvasGo;
    private static RectTransform m_mainCanvasRectTransform;
    #endregion

    #region
    public static GameObject uiCanvasGo
    {
        get
        {
            if (m_uiCanvasGo == null)
            {
                m_uiCanvasGo = GameObject.Find("uiCamera/uiCanvas");
                if (m_uiCanvasGo == null)
                {
                    Logger.e("ui canvas game object is null.");
                }
            }
            return m_uiCanvasGo;
        }
    }

    public static RectTransform uiCanvasRectTransform
    {
        get
        {
            if (uiCanvasGo == null)
            {
                throw new System.Exception("not found ui canvas rect transform");
            }
            if (m_uiCanvasRectTransform == null)
            {
                m_uiCanvasRectTransform = uiCanvasGo.GetComponent<RectTransform>();
            }
            return m_uiCanvasRectTransform;
        }
    }

    public static Camera mainCanvasCamera
    {
        get
        {
            if (m_mainCamera == null)
            {
                m_mainCamera = GameObject.Find("mainCamera").GetComponent<Camera>();
                if (m_mainCamera == null)
                {
                    Logger.e("main camera game object is null.");
                }
            }
            return m_mainCamera;
        }
    }

    public static MainCanvasRender mainCanvasRender
    {
        get
        {
            if (mainCanvasCamera == null)
            {
                throw new System.Exception("not found main canvas render.");
            }
            if (m_mainCanvasRender == null)
            {
                m_mainCanvasRender = mainCanvasCamera.GetComponent<MainCanvasRender>();
            }
            return m_mainCanvasRender;
        }
    }

    public static GameObject mainCanvasGo
    {
        get
        {
            if (m_mainCanvasGo == null)
            {
                m_mainCanvasGo = GameObject.Find("mainCamera/Canvas/mainCanvas");
                if (m_mainCanvasGo == null)
                {
                    Logger.e("main canvas game object is null.");
                }
            }
            return m_mainCanvasGo;
        }
    }

    public static RectTransform mainCanvasRectTransform
    {
        get
        {
            if (mainCanvasGo == null)
            {
                throw new System.Exception("not found main canvas rect transform.");
            }
            if (m_mainCanvasRectTransform == null)
            {
                m_mainCanvasRectTransform = mainCanvasGo.GetComponent<RectTransform>();
            }
            return m_mainCanvasRectTransform;
        }
    }
    #endregion

    public void Initialze()
    {
        UIManager.Instance.Initialize();
        UIManager.Instance.UICanvasTransform = uiCanvasRectTransform;
        UIManager.Instance.MainCanvasCamera = mainCanvasCamera;
        UIManager.Instance.MainCanvasRender = mainCanvasRender;
        UIManager.Instance.MainCanvasTransform = mainCanvasRectTransform;
        mainCanvasRender.Initialize();
        MainController.Instance.Initialize();
        InputManager.Instance.Initialize();
    }

    public void Preload()
    {

    }

    public void Load()
    {
        
    }

    public void Tick()
    {
        UIManager.Instance.Tick();
        MainController.Instance.Tick();
        InputManager.Instance.Tick();
    }

    public void LateTick()
    {

    }

    public void PreRender()
    {

    }

    public void PostRender()
    {
        mainCanvasRender.PostRender();
    }

    public void RenderImage(RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit(source, destination);
    }

    public void Uninitialize()
    {
        InputManager.Instance.Uninitialize();
        MainController.Instance.Uninitialize();
        mainCanvasRender.Uninitialize();
        UIManager.Instance.UICanvasTransform = null;
        UIManager.Instance.MainCanvasCamera = null;
        UIManager.Instance.MainCanvasRender = null;
        UIManager.Instance.MainCanvasTransform = null;
        UIManager.Instance.Uninitialize();

        base.Release();
    }
}
